<template>
  <div class="mod-config">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-input v-model="dataForm.keywords" placeholder="卡号" clearable></el-input>
      </el-form-item>
      <el-form-item label="充值卡状态">
				<el-select v-model="dataForm.status" placeholder="充值卡状态" clearable>
					<el-option label="全部" value="-1" ></el-option>
					<el-option label="未激活" value="0" ></el-option>
					<el-option label="已激活" value="1" ></el-option>
          <el-option label="锁定" value="2" ></el-option>
					<el-option label="已使用" value="4" ></el-option>
				</el-select>
			</el-form-item>
      <!-- <el-form-item label="使用状态">
				<el-select v-model="dataForm.isUsed" placeholder="充值卡状态">
					<el-option label="全部" value="" ></el-option>
					<el-option label="未使用" value="0" ></el-option>
					<el-option label="已使用" value="1" ></el-option>
				</el-select>
			</el-form-item> -->
      <el-form-item label="充值金额">
				<el-select v-model="dataForm.money" placeholder="充值金额" clearable>
					<el-option label="全部" value="0" ></el-option>
					<el-option label="正序" value="1" ></el-option>
					<el-option label="倒序" value="2" ></el-option>
				</el-select>
			</el-form-item>
      <el-form-item label="充值卡类型">
				<el-select v-model="dataForm.type" placeholder="充值卡类型" clearable>
					<el-option label="全部" value="" ></el-option>
					<el-option label="老卡充值" value="1" ></el-option>
					<el-option label="网红推荐卡号" value="2" ></el-option>
          <el-option label="2019年货节" value="3" ></el-option>
				</el-select>
			</el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
        <!-- <el-button v-if="isAuth('generator:shoppingrechargeablecard:save')" type="primary" @click="addOrUpdateHandle()">新增</el-button> -->
        <el-select v-model="dataForm.operationState" placeholder="操作类型" clearable>
					<el-option label="激活" value="1" ></el-option>
					<el-option label="锁定" value="2" ></el-option>
				</el-select>
        <el-button type="danger" @click="batch()" :disabled="this.dataForm.operationState === ''">批量操作</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%;">
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50">
      </el-table-column>
      <el-table-column
        prop="cardNum"
        header-align="center"
        align="center"
        label="充值卡卡号">
      </el-table-column>
      <el-table-column
        prop="password"
        header-align="center"
        align="center"
        label="密码">
      </el-table-column>
      <el-table-column
        prop="value"
        header-align="center"
        align="center"
        label="充值卡面额">
        <template slot-scope="scope">
          <span v-if="scope.row.value != null">￥ {{scope.row.value}}</span>
          <span v-if="scope.row.value == null">￥ 0</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="type"
        header-align="center"
        align="center"
        label="充值卡类型">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.type == 1" type="success">老卡充值</el-tag>
          <el-tag v-if="scope.row.type == 2" type="success">网红推荐卡号</el-tag>
          <el-tag v-if="scope.row.type == 3" type="success">2019年货节</el-tag>
          <el-tag v-if="scope.row.type == ''" type="success">未知</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="addTime"
        header-align="center"
        align="center"
        label="添加时间">
      </el-table-column>
      <el-table-column
        prop="status"
        header-align="center"
        align="center"
        label="充值卡状态">
        <template slot-scope="scope">
          <!-- <span v-if="scope.row.status == 0">未激活</span> -->
          <span v-if="scope.row.status == 1">正常</span>
          <span v-if="scope.row.status == 2">锁定</span>
          <span v-if="scope.row.status == 4">已使用</span>
          <el-button v-if="scope.row.status == 0" type="primary" @click="batch(scope.row.id, 1)">激活</el-button><br/>
          <el-button v-if="scope.row.status == 0" type="danger" @click="batch(scope.row.id, 2)">锁定</el-button>
        </template>
      </el-table-column>
      <el-table-column
        prop="isUsed"
        header-align="center"
        align="center"
        label="使用状态">
        <template slot-scope="scope">
          <span v-if="scope.row.isUsed == false">未使用</span>
          <span v-else-if="scope.row.isUsed == true">已使用</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="userName"
        header-align="center"
        align="center"
        label="充值账号">
      </el-table-column>
      <el-table-column
        prop="trueName"
        header-align="center"
        align="center"
        label="充值名称">
      </el-table-column>
      <el-table-column
        prop="useDate"
        header-align="center"
        align="center"
        label="使用日期">
      </el-table-column>
      <!-- <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="150"
        label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="addOrUpdateHandle(scope.row.id)">修改</el-button>
          <el-button type="text" size="small" @click="deleteHandle(scope.row.id)">删除</el-button>
        </template>
      </el-table-column> -->
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
  </div>
</template>

<script>
  // import AddOrUpdate from './shoppingrechargeablecard-add-or-update'
  export default {
    data () {
      return {
        dataForm: {
          keywords: '',
          money: '',
          isUsed: '',
          status: '',
          type: '',
          operationState: ''
        },
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false
      }
    },
    // components: {
    //   AddOrUpdate
    // },
    activated () {
      this.getDataList()
    },
    methods: {
      // 获取数据列表
      getDataList () {
        this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl('/card/queryCardList'),
          method: 'get',
          params: this.$http.adornParams({
            'page': this.pageIndex,
            'limit': this.pageSize,
            'keywords': this.dataForm.keywords,
            'money': this.dataForm.money,
            'isUsed': this.dataForm.isUsed,
            'status': this.dataForm.status,
            'type': this.dataForm.type
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.dataList = data.data.list
            this.totalPage = data.data.totalCount
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
      // 新增 / 修改
      // addOrUpdateHandle (id) {
      //   this.addOrUpdateVisible = true
      //   this.$nextTick(() => {
      //     this.$refs.addOrUpdate.init(id)
      //   })
      // },
      // 删除
      deleteHandle (id) {
        var ids = id ? [id] : this.dataListSelections.map(item => {
          return item.id
        })
        this.$confirm(`确定对[id=${ids.join(',')}]进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl('/generator/shoppingrechargeablecard/delete'),
            method: 'post',
            data: this.$http.adornData(ids, false)
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        })
      },
      batch (id, val) {
        var ids = id ? [id] : this.dataListSelections.map(item => {
          return item.id
        })
        if (ids.length === 0) {
          this.$message.error('请选择数据')
          return
        }
        let type = val || this.dataForm.operationState
        this.$confirm(`确定进行操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl('/card/modifyRechargeableCardStatusById/' + type + ''),
            method: 'post',
            data: this.$http.adornData(ids, false)
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        })
      }
    }
  }
</script>
